﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;


namespace DA1
{
    public partial class ThanhPham : Form
    {
        public ThanhPham()
        {
            InitializeComponent();
        }
        private void Load_ManHinh()
        {
            try
            {
                ThanhPham_BUS bus = new ThanhPham_BUS();
                DGV_DanhSachTP.DataSource = bus.GetDanhSachTP();
                txtMaTP.Text = "";
                txtTenTP.Text = "";
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }

        }
        private void ThanhPham_Load(object sender, EventArgs e)
        {
            try
            {
                ThanhPham_BUS bus = new ThanhPham_BUS();
                cmbLoaiTP.DataSource = bus.GetCmbLoaiTP();
                cmbLoaiTP.DisplayMember = "TenLoai1";
                cmbLoaiTP.ValueMember = "MaLoai";

                cmb_LoaiTP_sua.DataSource = bus.GetCmbLoaiTP();
                cmb_LoaiTP_sua.DisplayMember = "TenLoai1";
                cmb_LoaiTP_sua.ValueMember = "MaLoai";

                cmb_LoaiTP_Xem.DataSource = bus.GetCmbLoaiTP();
                cmb_LoaiTP_Xem.DisplayMember = "TenLoai1";
                cmb_LoaiTP_Xem.ValueMember = "MaLoai";

                cmb_LoaiTP_NLChoTP.DataSource = bus.GetCmbLoaiTP();
                cmb_LoaiTP_NLChoTP.DisplayMember = "TenLoai1";
                cmb_LoaiTP_NLChoTP.ValueMember = "MaLoai";

                cmb_LoaiTP_Sua_Seacrh.DataSource = bus.GetCmbLoaiTP();
                cmb_LoaiTP_Sua_Seacrh.DisplayMember = "TenLoai1";
                cmb_LoaiTP_Sua_Seacrh.ValueMember = "MaLoai";
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }
        private void btn_ThemTP_Click(object sender, EventArgs e)
        {
            Lbl_ThongBao.Visible = false;
            if (txtTenTP.Text == "")
            {
                Lbl_ThongBao.Text = "Hãy nhập tên thành phẩm";
                Lbl_ThongBao.Visible = true;
                return;
            }              
            else
            {
                ThanhPham_BUS bus = new ThanhPham_BUS();
                ThanhPham_DTO dto = new ThanhPham_DTO();
                try
                {
          
                    dto.TenTP = txtTenTP.Text.ToString();
                    dto.MaLoaiTP = int.Parse(cmbLoaiTP.SelectedValue.ToString());
                   // dto.MaXe = int.Parse(cmb_Xe.SelectedValue.ToString());
                    dto.SoLuong = int.Parse(txt_SoLuong.Text.ToString());

                    dto.MaTP = bus.proc_Ins_ThanhPham(dto);
                    txtMaTP.Text = dto.MaTP.ToString();

                    Lbl_ThongBao.Text = "Thêm thành công";
                    Lbl_ThongBao.Visible = true;
                    Load_ManHinh();
                }
                 catch (Exception ex)
                {
                    MessageBox.Show("Không thêm được do: " + ex);
                    Lbl_ThongBao.Visible = false;
                }
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                ThanhPham_BUS bus = new ThanhPham_BUS();
                DGV_DanhSachTP.DataSource = bus.GetDanhSachTPTheoXe(int.Parse(cmb_LoaiTP_Xem.SelectedValue.ToString()));
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void DGV_DanhSachTP_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                int i = DGV_DanhSachTP.CurrentRow.Index;
                if ((e.RowIndex == i) && (e.ColumnIndex == -1))
                {
                    Lbl_ThongBao.Visible = false;
                    txt_MaTP_Sua.Text = DGV_DanhSachTP.Rows[i].Cells["MaTP"].Value.ToString();
                    txt_TenTP_Sua.Text = DGV_DanhSachTP.Rows[i].Cells["TenTP"].Value.ToString();
                    cmb_LoaiTP_sua.DisplayMember = DGV_DanhSachTP.Rows[i].Cells["TenLoaiTP"].Value.ToString();
                    //  cmb_LoaiTP_sua.ValueMember = DGV_DanhSachTP.Rows[i].Cells["MaLoaiTP"].Value.ToString();
                   // cmb_Xe_Sua.DisplayMember = DGV_DanhSachTP.Rows[i].Cells["TenXe"].Value.ToString();
                    // cmb_Xe_Sua.ValueMember=DGV_DanhSachTP.Rows[i].Cells["MaXe"].Value.ToString();
                    txt_SoLuong_Sua.Text = DGV_DanhSachTP.Rows[i].Cells["SoLuong"].Value.ToString();
                }
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }
        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                ThanhPham_DTO dto = new ThanhPham_DTO();
                ThanhPham_BUS bus = new ThanhPham_BUS();
                dto.MaTP = int.Parse(txt_MaTP_Sua.Text.ToString());
                dto.TenTP = txt_TenTP_Sua.Text.ToString();
                dto.MaLoaiTP = int.Parse(cmb_LoaiTP_sua.SelectedValue.ToString());
                dto.SoLuong = int.Parse(txt_SoLuong_Sua.Text.ToString());
                bus.SuaThanhPham(dto);
                Lbl_ThongBao.Text = "Sửa thành công";
                Lbl_ThongBao.Visible = true;
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void cmb_LoaiTP_NLChoTP_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                cmb_TP_NLChoTP.Text = "";
                ThanhPham_BUS bus = new ThanhPham_BUS();
                int MaLoaiTP = int.Parse(cmb_LoaiTP_NLChoTP.SelectedValue.ToString());
                cmb_TP_NLChoTP.DataSource = bus.GetDanhSachTPTheoMaLoaiTP(MaLoaiTP);
                cmb_TP_NLChoTP.DisplayMember = "TenTP";
                cmb_TP_NLChoTP.ValueMember = "MaTP";
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }
        private void cmb_TP_NLChoTP_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                ThanhPham_BUS bus = new ThanhPham_BUS();
                DGV_NGChoTP.DataSource = bus.GetDanhSachNL();
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void btn_ChonNLChoTP_Click(object sender, EventArgs e)
        {
            try
            {
                TS_CT_TP_BUS bus = new TS_CT_TP_BUS();
                TS_CT_TP_DTO dto = new TS_CT_TP_DTO();
                //Duyet dataGridView và dong nao co dau check thi insert xuong DB
                for (int i = 0; i < DGV_NGChoTP.Rows.Count; i++)
                {
                    if (DGV_NGChoTP.Rows[i].Cells["Check_NLG"].Value == null)
                        DGV_NGChoTP.Rows[i].Cells["Check_NLG"].Value = "false";
                    bool CheckBox = bool.Parse(DGV_NGChoTP.Rows[i].Cells["Check_NLG"].Value.ToString());
                    if (CheckBox == true)
                    {
                        
                        dto.MaTP = int.Parse(cmb_TP_NLChoTP.SelectedValue.ToString());
                        dto.MaNLG = int.Parse(DGV_NGChoTP.Rows[i].Cells["MaNLG"].Value.ToString());
                        if (DGV_NGChoTP.Rows[i].Cells["SoLuongNLG"].Value == null)
                        {
                            MessageBox.Show("Hãy nhập số lượng ");
                            return;
                        }
                        dto.SoLuong = float.Parse(DGV_NGChoTP.Rows[i].Cells["SoLuongNLG"].Value.ToString());
                        bus.proc_Ins_SoLuongNLChoMoiTP(dto);
                    }
                }
                Lbl_ThongBao.Text = "Chọn thành công";
                Lbl_ThongBao.Visible = true;
                DGV_NLCuaTP_Sua_Search.DataSource = bus.GetNLCuaTP(int.Parse(cmb_TP_NLChoTP.SelectedValue.ToString()));
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void cmb_LoaiTP_Sua_Seacrh_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                Lbl_ThongBao.Visible = false;
                cmb_TP_Sua_Search.Text = "";
                ThanhPham_BUS bus = new ThanhPham_BUS();
                int MaLoaiTP = int.Parse(cmb_LoaiTP_Sua_Seacrh.SelectedValue.ToString());
                cmb_TP_Sua_Search.DataSource = bus.GetDanhSachTPTheoMaLoaiTP(MaLoaiTP);
                cmb_TP_Sua_Search.DisplayMember = "TenTP";
                cmb_TP_Sua_Search.ValueMember = "MaTP";
               
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi ";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void btn_Search_NLCuaTP_Click(object sender, EventArgs e)
        {
            try
            {

                Lbl_ThongBao.Visible = false;
                TS_CT_TP_BUS bus = new TS_CT_TP_BUS();
                DGV_NLCuaTP_Sua_Search.DataSource = bus.GetNLCuaTP(int.Parse(cmb_TP_Sua_Search.SelectedValue.ToString()));
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi rùi: ";
                Lbl_ThongBao.Visible = true;
            }
        }

        private void btn_Sua_NLCuaTP_Click(object sender, EventArgs e)
        {


            try
            {
                //Duyet dataGridView và dong nao co dau check thi insert xuong DB
                for (int i = 0; i < DGV_NLCuaTP_Sua_Search.Rows.Count; i++)
                {
                    if (DGV_NLCuaTP_Sua_Search.Rows[i].Cells["CheckBox"].Value == null)
                        DGV_NLCuaTP_Sua_Search.Rows[i].Cells["CheckBox"].Value = "false";
                    bool CheckBox = bool.Parse(DGV_NLCuaTP_Sua_Search.Rows[i].Cells["CheckBox"].Value.ToString());
                    if (CheckBox == true)
                    {
                        TS_CT_TP_BUS bus = new TS_CT_TP_BUS();
                        TS_CT_TP_DTO dto = new TS_CT_TP_DTO();
                        dto.MaTP = int.Parse(cmb_TP_Sua_Search.SelectedValue.ToString());
                        dto.MaNLG = int.Parse(DGV_NLCuaTP_Sua_Search.Rows[i].Cells["MaNL"].Value.ToString());
                        dto.SoLuong = float.Parse(DGV_NLCuaTP_Sua_Search.Rows[i].Cells["SoLuongNL"].Value.ToString());
                        bus.Up_SoLuongNLCuaTP(dto);
                        Lbl_ThongBao.Text = "Sửa thành công";
                        Lbl_ThongBao.Visible = true;
                        //Load lai dl vao DataGridView
                        DGV_NLCuaTP_Sua_Search.DataSource = bus.GetNLCuaTP(int.Parse(cmb_TP_Sua_Search.SelectedValue.ToString()));
                    }

                }
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi: " + ex.ToString();
                Lbl_ThongBao.Visible = true;
            }

        }

        private void btn_Xoa_NL_Sua_Search_Click(object sender, EventArgs e)
        {
            try
            {
                //Duyet dataGridView và dong nao co dau check thi insert xuong DB
                for (int i = 0; i < DGV_NLCuaTP_Sua_Search.Rows.Count; i++)
                {
                    if (DGV_NLCuaTP_Sua_Search.Rows[i].Cells["CheckBox"].Value == null)
                        DGV_NLCuaTP_Sua_Search.Rows[i].Cells["CheckBox"].Value = "false";
                    bool CheckBox = bool.Parse(DGV_NLCuaTP_Sua_Search.Rows[i].Cells["CheckBox"].Value.ToString());
                    if (CheckBox == true)
                    {

                        TS_CT_TP_BUS bus = new TS_CT_TP_BUS();
                        TS_CT_TP_DTO dto = new TS_CT_TP_DTO();
                        dto.MaTP = int.Parse(cmb_TP_Sua_Search.SelectedValue.ToString());
                        dto.MaNLG = int.Parse(DGV_NLCuaTP_Sua_Search.Rows[i].Cells["MaNL"].Value.ToString());
                        dto.SoLuong = float.Parse(DGV_NLCuaTP_Sua_Search.Rows[i].Cells["SoLuongNL"].Value.ToString());
                        bus.Del_SoLuongNLCuaTP(dto);
                        Lbl_ThongBao.Text = "Xóa thành công";
                        Lbl_ThongBao.Visible = true;
                        DGV_NLCuaTP_Sua_Search.DataSource = bus.GetNLCuaTP(int.Parse(cmb_TP_Sua_Search.SelectedValue.ToString()));
                    }

                }
            }
            catch (Exception ex)
            {
                Lbl_ThongBao.Text = "Lỗi: " + ex.ToString();
                Lbl_ThongBao.Visible = true;
            }

        }

        



    }
}
